package com.tracy.life.discovery.adapter;

/**
 * Created by asus on 2016/7/20.
 */

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;

import com.tracy.life.R;
import com.tracy.life.bean.SpecialBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 攻略上面四张图片的适配器
 */
public class HotRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<SpecialBean.DataBean.GroupsBean> mSpecialList;
    private List<String> mURlStringList;

    private int TYPE_HOT = 0;
    private int TYPE_SPECIAL = 1;


    public HotRecyclerViewAdapter(Context context) {
        this.mContext = context;
    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HOT;
        }
        return TYPE_SPECIAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HOT) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_hot_recycler, parent, false);
            return new HotVieHolder(view);
        }
        if (viewType == TYPE_SPECIAL) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_special, parent, false);
            SpecialViewHolder viewHolder = new SpecialViewHolder(view);
            return viewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (type == TYPE_HOT) {
            HotVieHolder hotVieHolder = (HotVieHolder) holder;
            hotVieHolder.gridView.setAdapter(new GridViewAdapter(mContext, mURlStringList));
        }
        if (type == TYPE_SPECIAL) {
            SpecialViewHolder viewHolder = (SpecialViewHolder) holder;
            viewHolder.textView.setText(mSpecialList.get(position-1).name);

            ArrayList<String> mUrlList = new ArrayList<String>();
            List<SpecialBean.DataBean.GroupsBean.ChannelsBean> list = mSpecialList.get(position - 1).channels;
            for (int i = 0; i < list.size(); i++) {
                String iconUrl = list.get(i).icon_url;
                mUrlList.add(iconUrl);
            }

            viewHolder.gridView.setAdapter(new GridViewAdapter(mContext,mUrlList ));

        }
    }

    @Override
    public int getItemCount() {
        return (mURlStringList == null || mSpecialList==null) ? 0 : mSpecialList.size()+1;
    }

    /**
     * 热门推荐的ViewHolder
     */
    class HotVieHolder extends RecyclerView.ViewHolder {

        public GridView gridView;

        public HotVieHolder(View itemView) {
            super(itemView);
            gridView = (GridView) itemView.findViewById(R.id.grid_view);
        }
    }

    /**
     * 专区的viewHolder
     */
    class SpecialViewHolder extends RecyclerView.ViewHolder {
        private GridView gridView;
        private TextView textView;

        public SpecialViewHolder(View itemView) {
            super(itemView);
            gridView = (GridView) itemView.findViewById(R.id.special_grid_view);
            textView = (TextView) itemView.findViewById(R.id.tv_name);
        }
    }


    // 提供给外部设置数据的方法
    public void setDatas(List<String> data, List<SpecialBean.DataBean.GroupsBean> list) {
        this.mURlStringList = data;
        this.mSpecialList = list;
        notifyDataSetChanged();
    }

}
